﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using ShareDesigner.Web.Class;
using System.Xml.Linq;
using System.IO;
using System.Data.SqlClient;
namespace ShareDesigner.Web.Services
{
    /// <summary>
    /// Summary description for WorkFlow
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class WorkFlow : System.Web.Services.WebService
    {
     
        [WebMethod]
        public string GetWorkFlowList()
        {
           // return "";
            string xml = "";
            using (ShareDesigner.Web.Class.WorkFlowDataContext dataContext = new WorkFlowDataContext())
            {

                try
                {

                    XDocument doc = new XDocument(
     new XElement("WorkFlows", from p in dataContext.WorkFlow_Processes
                               select (new XElement("WorkFlow",
                                       new XAttribute("Name",string.IsNullOrEmpty( p.WorkFlowName)?p.WorkFlowID:p.WorkFlowName),
                                       new XAttribute("ID", p.WorkFlowID)))));
                    xml = doc.ToString();

                }
                catch (Exception e)
                {
                    xml = "";
                }
            }

            return xml;
        }

        /// <summary>
        /// 获取工作流xml描述
        /// </summary>
        /// <param name="workflowID">流程ID</param>
        /// <returns></returns>
        [WebMethod]
        public string GetWorkFlowXML(string workflowID)
        {
            string xml = "";
            using (ShareDesigner.Web.Class.WorkFlowDataContext dataContext = new WorkFlowDataContext())
            {

                try
                {

                    WorkFlow_Process process = dataContext.WorkFlow_Processes.Single(p => p.WorkFlowID == workflowID);

                    if (process != null)
                        xml = process.WorkFlowXML;
                }
                catch (Exception e)
                {
                    xml = "";
                }
            }

            return xml;
        }
        void deleteExistsWorkFlow(string workflowID)
        {
            string deleteSql = @"delete from WorkFlow_Rule 
where BeginActivityID in(select ActivityID from WorkFlow_Activity where workflowID='{0}')
or EndActivityID in (select ActivityID from WorkFlow_Activity where workflowID='{0}');
delete from WorkFlow_Activity where workflowID='{0}';
delete from WorkFlow_Process where workflowID='{0}';";
            using (ShareDesigner.Web.Class.WorkFlowDataContext dataContext = new WorkFlowDataContext())
            {
                deleteSql = string.Format(deleteSql, workflowID);
                dataContext.ExecuteCommand(deleteSql, workflowID);
                dataContext.SubmitChanges();
            }

        }
        /// <summary>
        /// 更新流程xml
        /// </summary>
        /// <param name="workFlowXml">流程xml描述</param>
        [WebMethod]
        public void UpdateWorkFlowXML(string workFlowXml)
        {

            using (ShareDesigner.Web.Class.WorkFlowDataContext dataContext = new WorkFlowDataContext())
            {
                WorkFlow_Process process = new WorkFlow_Process();

                Byte[] b = System.Text.UTF8Encoding.UTF8.GetBytes(workFlowXml);
                XElement xele = XElement.Load(System.Xml.XmlReader.Create(new MemoryStream(b)));

                process.WorkFlowName = xele.Attribute(XName.Get("Name")).Value;
                process.WorkFlowID = xele.Attribute(XName.Get("UniqueID")).Value;
                process.WorkFlowXML = workFlowXml;
                deleteExistsWorkFlow(process.WorkFlowID);
                var partNos = from item in xele.Descendants("Activity") select item;
                foreach (XElement node in partNos)
                {

                    WorkFlow_Activity activity = new WorkFlow_Activity();
                    process.WorkFlow_Activities.Add(activity);
                    activity.ActivityID = node.Attribute(XName.Get("UniqueID")).Value;
                    activity.ActivityName = node.Attribute(XName.Get("ActivityName")).Value;
                    activity.ActivityType = node.Attribute(XName.Get("Type")).Value;
                }

                dataContext.WorkFlow_Processes.InsertOnSubmit(process);
                partNos = from item in xele.Descendants("Rule") select item;
                WorkFlow_Rule rule = null;
                foreach (XElement node in partNos)
                {
                    rule = new WorkFlow_Rule();
                    rule.BeginActivityID = node.Attribute(XName.Get("BeginActivityUniqueID")).Value;
                    rule.Condition = node.Attribute(XName.Get("RuleCondition")).Value;
                    rule.EndActivityID = node.Attribute(XName.Get("EndActivityUniqueID")).Value;
                    rule.RuleID = node.Attribute(XName.Get("UniqueID")).Value;
                    rule.RuleName = node.Attribute(XName.Get("RuleName")).Value;
                    rule.RuleType = node.Attribute(XName.Get("LineType")).Value;
                    dataContext.WorkFlow_Rules.InsertOnSubmit(rule);

                }
                dataContext.SubmitChanges();
            }


        }
    }
}
